package com.hh.tc.ui.todo

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.SavedStateHandle
import com.hh.base.ui.BaseViewModel
import com.hh.base.util.ext.DateType
import com.hh.base.util.ext.toShow
import com.hh.db.core.ObjectBoxUtil
import com.hh.db.core.ObjectBoxUtil.queryFirst
import com.hh.db.core.ObjectBoxUtil.queryList
import com.hh.db.entity.TODODetail
import com.hh.db.entity.TODODetail_
import com.hh.tc.ui.todo.addItem.AddItemDialog
import com.hh.tc.ui.todo.editItem.EditTodoItemActivity
import java.util.Date

/**
 * Create By hHui on 2025/4/16 9:25
 *
 * @description
 */
class TodoViewModel(handle: SavedStateHandle) : BaseViewModel(handle), AddItemDialog.OnItemListener {
    val toDayTodoList = ObservableArrayList<TODODetail>()

    override fun initData() {
        tryBuildTodayTodoDetails()
    }

    private fun tryBuildTodayTodoDetails() {
        val dayStr = Date().toShow(DateType.DAY)
        ObjectBoxUtil.todoDetailBox.queryList(TODODetail_.toExecuteDay.equal(dayStr)).apply {
            // 今日代办还未创建--->创建今日的代办
            if (isEmpty()) {
                val list = ArrayList<TODODetail>()
                ObjectBoxUtil.todoItemBox.all.onEach { item ->
                    if (item.timesPerDay > 1) {
                        for (i in 0 until item.timesPerDay) {
                            val detail = TODODetail()
                            detail.toExecuteDay = Date().toShow(DateType.DAY)
                            detail.itemId = item.id
                            detail.item = item
                            detail.sort = i + 1
                            detail.itemName = "${item.name}(第${i + 1}次)"
                            list.add(detail)
                        }
                    } else {
                        val detail = TODODetail()
                        detail.toExecuteDay = Date().toShow(DateType.DAY)
                        detail.item = item
                        detail.itemId = item.id
                        detail.itemName = item.name
                        list.add(detail)
                    }
                }
                ObjectBoxUtil.todoDetailBox.put(list)
            }
        }
        refreshData()
    }

    private fun refreshData() {
        // 显示今日待办
        ObjectBoxUtil.todoDetailBox.queryList(TODODetail_.toExecuteDay.equal(Date().toShow(DateType.DAY))).let { list ->
            list.sortBy { it.sort }
            toDayTodoList.clear()
            toDayTodoList.addAll(list)
        }
    }

    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        refreshData()
    }

    override fun onSuccess() {
        refreshData()
    }


    override fun onError(message: String) {
        alertMessage(message)
    }

    fun editClick() {
        jump2Activity(EditTodoItemActivity::class.java, false)
    }

    fun executeClick(detail: TODODetail) {
        ObjectBoxUtil.todoDetailBox.queryFirst(
            TODODetail_.itemId.equal(detail.itemId)
                .and(TODODetail_.toExecuteDay.equal(Date().toShow(DateType.DAY)))
                .and(TODODetail_.sort.less(detail.sort))
                .and(TODODetail_.executeAt.isNull)
        ).apply {
            if (this != null) {
                alertTitleAndMessage(msg = "您还没有完成${this.itemName}!")
            } else {
                showDateTimerPicker({ date, v ->
                    detail.executeAt = date.toShow()
                    ObjectBoxUtil.todoDetailBox.put(detail)
                    refreshData()
                }, array = booleanArrayOf(false, false, false, true, true, false))
            }
        }
    }
}